/**
 * 全民营销首页
 */
define(function (require, exports, module) {
    var $ = require("jquery");
    var util = require("util");
    var ui = require("ui");
    var angular = require("angular");
    var dialog = require("dialog");
    require("angular-ui");
    require('permission');
    require('authorize');
    require("./select_project");
    require('global');

    var app = angular.module('app', ["ngUI", "selectProjectSrv"]);
    var api_prefix = G_CONFIG["api_prefix"] + "project/auth/"; //api前缀
    var token = util.getToken();

    // 查询服务
    function filterService($http) {
        var service = {
            data: [],
            // 查询参数
            queryParam: {
                token: token
            },
            doQuery: function (pageIndex) {
                var url = api_prefix + 'get-app-projects-by-packages';
                $http.get(url, {params: service.queryParam})
                    .success(function (data) {
                        if (data.retCode == 0) {
                            service.data = data.data;
                        } else {
                            ui.showErrTip(data.errMsg);
                        }
                    })
            },
            // 删除绑定的项目
            delete: function (list, id) {
                for (var i = 0, len = list.length; i < len; i++) {
                    if (id === list[i].id) {
                        list.splice(i, 1);
                        break
                    }
                }
            },
            // 保存
            save: function (data) {
                var _this = this;
                var params = this.getRecord(data);
                var url = api_prefix + 'save-bind-app-projects';
                $http.post(url, params)
                    .success(function (data) {
                        if (data.retCode == 0) {
                            var hasBusinessData = data.data.hasbusinessdata && data.data.hasbusinessdata.length > 0;
                            var cannotAddData = data.data.cannotadddata && data.data.cannotadddata.length > 0;
                            
                            if (hasBusinessData || cannotAddData) {
                                var msg = '';

                                if (hasBusinessData) {
                                    var deleteMsg = _this.cannotMsg(data.data.hasbusinessdata);
                                    msg += '<div><span class="c-orange">' + deleteMsg + ' </span>已产生业务数据，无法删除。</div>';
                                }

                                if (cannotAddData) {
                                    if(hasBusinessData){
                                        msg += '<div style="border-top: 1px dashed #ccc; margin: 8px 0;"></div>';
                                    }
                                    var deleteMsg = _this.cannotMsg(data.data.cannotadddata);
                                    var noWcbPhone = _.filter(data.data.cannotadddata, function (item) {
                                        return item.ApplicationCode == '5030' && item.flag;//项目没有绑定旺财宝话机
                                    });
                                    if(noWcbPhone.length>0){
                                        var noWcbPhoneMsg = _this.cannotMsg(noWcbPhone);
                                        msg+='<div><span class="c-orange">' + noWcbPhoneMsg + ' </span>项目没有绑定旺财宝话机</div>';
                                    }
                                    msg += '<div><span class="c-orange">' + deleteMsg + ' </span>项目绑定出现异常，请联系云客运营400QQ客服：4008333870</div>';
                                }

                                util.alert(msg);
                                _this.doQuery();
                            } else {
                                ui.showSucTip("保存成功");
                                setTimeout(function () {
                                    location.href = "/index.shtml"
                                }, 2000);
                            }
                        } else {
                            ui.showErrTip(data.errMsg);
                        }
                    })
            },
            cannotMsg: function (arr) {
                var msg = '', projArr = [];
                for (var i = 0, len = arr.length; i < len; i++) {
                    projArr.push(arr[i].ProjectName);
                }
                msg = projArr.join("、");
                return msg;
            },
            // 收集要保存的数据
            getRecord: function (data) {
                var arr = [];
                for (var i = 0, len = data.length; i < len; i++) {
                    var item = data[i];
                    var list = item.list;
                    for (var k = 0, len3 = list.length; k < len3; k++) {
                        var record = {};
                        record.ApplicationId = item.id;
                        record.ApplicationCode = item.appcode;
                        record.ProjectId = list[k].id;
                        arr.push(record);
                    }
                }
                var obj = {
                    token: token,
                    appProjectList: arr
                };
                return obj;
            }
        };
        return service;
    }

    app.factory('filters', ['$http', filterService]);

    function queryService($scope, filters, projService) {
        filters.doQuery();
        $scope.query = filters;
        $scope.delete = function (list, id) {
            filters.delete(list, id);
        };
        $scope.save = function (data) {
            filters.save(data);
        };
        $scope.selectProjDialog = function (data, dialog, dialogScope) {
            projService.selectProjDialog(data, dialog, dialogScope);
        };
        $scope.saveSelect = function (data) {
            projService.confirm(data,function(){
                $scope.$applyAsync();
            });
        };
    }

    app.controller('queryService', ['$scope', 'filters', 'projService', queryService]);

    angular.bootstrap(document, ['app']);
});